package com.pysoft.web.controller.safe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pysoft.safe.dao.entity.ResourceEntity;
import com.pysoft.safe.service.ResourceService;

@Controller
@RequestMapping("/safe/resource")
public class ResourceController {

	@Autowired
	private ResourceService resourceService;

	@RequestMapping(value="/show.action")
	public String show() {
		return "safe/resource";
	}

	@RequestMapping(value="/tree.action")
	@ResponseBody
	public List<ResourceEntity> tree() {
		List<ResourceEntity> tree = new ArrayList<ResourceEntity>();
		ResourceEntity root = new ResourceEntity();
		root.setId("root");
		root.setTitle("系统资源树");
		root.setName("系统资源树");
		root.setType("root");
		root.setPid("0");
		tree.add(root);
		List<ResourceEntity> resources = resourceService.query(null);
		tree.addAll(resources);
		return tree;
	}

	@RequestMapping(value="/add.action")
	@ResponseBody
	public ResourceEntity add(String pid, String type) {
		ResourceEntity entity = new ResourceEntity();
		entity.setPid(pid);
		entity.setType(type);
		if ("application".equals(type)) {
			entity.setIcon("../../image/icon/application.png");
		} else if ("module".equals(type)) {
			entity.setIcon("../../image/icon/module.png");
		} else if ("operation".equals(type)) {
			entity.setIcon("../../image/icon/operation.png");
		}
		ResourceEntity resource = resourceService.addResource(entity);
		return resource;
	}

	@RequestMapping(value="/remove.action")
	@ResponseBody
	public Map<String, Object> remove(@RequestParam("id") String id) {
		Map<String, Object> map = new HashMap<String, Object>(1);
		resourceService.removeResource(id);
		map.put("success", "1");
		return map;
	}

	@RequestMapping(value="/modify.action")
	@ResponseBody
	public Map<String, Object> modify(ResourceEntity resource) {
		System.out.println(resource);
		Map<String, Object> map = new HashMap<String, Object>(1);
		resourceService.modifyResource(resource);
		map.put("success", "1");
		return map;
	}
	
	@RequestMapping(value="/relating.action")
	@ResponseBody
	public Map<String, Object> relatingResource(@RequestParam("resourceId") String resourceId,  @RequestParam("roleIdList") List<String> roleIdList) {
		Map<String, Object> map = new HashMap<String, Object>(1);
		resourceService.relatingRole(resourceId, roleIdList);
		map.put("success", "1");
		return map;
	}
	
	@RequestMapping(value="/cancelRelating.action")
	@ResponseBody
	public Map<String, Object> cancelRelatingResource(@RequestParam("resourceId") String resourceId) {
		Map<String, Object> map = new HashMap<String, Object>(1);
		resourceService.cancelRelatingRole(resourceId);
		map.put("success", "1");
		return map;
	}
	
	@RequestMapping(value="/getRelatingRole.action")
	@ResponseBody
	public List<String> getRelatingResources(String resourceId){
		return resourceService.getRelatingRoles(resourceId);
	}	
}
